package org.beanstech.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import javax.persistence.PrePersist;

import org.beanstech.dao.service.AbstractDao;
import org.beanstech.persistence.Tblbook;
import org.beanstech.persistence.Tbluser;
import org.beanstech.util.StaticHibernateUtil;

public class BookDao extends AbstractDao {

	Connection conn = null;
	public int insertBook(Tblbook book, int userid)
	{
		int bookid = 0;
		try{
			System.out.println("User "+userid);
			book.setListdate(new Date());
			book.setStatus(0);
			bookid = insert(book);
			Tbluser user = (Tbluser)getObject(Tbluser.class, userid);
			user.getBooks().add((Tblbook)getObject(Tblbook.class, bookid));
			update(user);
			
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return bookid;
	}
	
	public ResultSet BookSearch(String bookname, String author, int subject){
		ResultSet rs = null;
		
		
		try {
			conn = StaticHibernateUtil.getHibernateUtil().getSessionTL().connection();
			CallableStatement call = conn.prepareCall("{call sp_book_search(?,?,?)}");
			call.setString(1, bookname);
			call.setString(2, author);
			call.setInt(3, subject);
			rs = call.executeQuery();
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		
		return rs;
	}
}
